﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace BusinessLayer
{
    public static class ProfissaoActions
    {
        public static bool CriarProfissao(String designacao)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into Profissao(idProf,designacaoProf) values((select Max(Profissao.idProf) from Profissao)+1,@designacao)";
                cmd.Parameters.AddWithValue("@designacao", designacao);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (Exception e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool AssociarProfissaoDepartamento(String idProf, String idOfic, String idDep)
        {
            ConnectionFactory con = new ConnectionFactory();
            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into ProfPorDep values(@profissao, @departamento, @oficina)";
                cmd.Parameters.AddWithValue("@profissao", idProf);
                cmd.Parameters.AddWithValue("@departamento", idDep);
                cmd.Parameters.AddWithValue("@oficina", idOfic);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message);  return false; }
           
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
    }
}
